package co.infinum.goldfinger.example.slice;

import co.infinum.goldfinger.example.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;

/**
 * ex.
 *
 * @author zh
 * @since 2021-06-25
 */
public class ChooseImplementationAbilitySlice extends AbilitySlice {
    private Component pinLoginExampleButton;
    private Component pinLoginRxExampleButton;
    private Component paymentExampleButton;
    private Component paymentRxExampleButton;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        SharedPrefs.init(this);
        super.setUIContent(ResourceTable.Layout_ability_choose_implementation);
        initViews();
        initListeners();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initListeners() {
        this.paymentExampleButton.setClickedListener(v -> navigateToPaymentAbility(false));
        this.paymentRxExampleButton.setClickedListener(v -> navigateToPaymentAbility(true));
        this.pinLoginExampleButton.setClickedListener(v -> navigateToSetPinAbility(false));
        this.pinLoginRxExampleButton.setClickedListener(v -> navigateToSetPinAbility(true));
    }

    private void initViews() {
        this.paymentExampleButton = findComponentById(ResourceTable.Id_paymentExampleButton);
        this.paymentRxExampleButton = findComponentById(ResourceTable.Id_paymentRxExampleButton);
        this.pinLoginExampleButton = findComponentById(ResourceTable.Id_pinLoginExampleButton);
        this.pinLoginRxExampleButton = findComponentById(ResourceTable.Id_pinLoginRxExampleButton);
    }

    private void navigateToPaymentAbility(boolean isRxExample) {
        Class<?> cls = isRxExample ? RxPaymentAbility.class : PaymentAbility.class;
        Intent intent = new Intent();
        // 通过Intent中的OperationBuilder类构造operation对象，指定设备标识（空串表示当前设备）、应用包名、Ability名称
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("co.infinum.goldfinger.example")
                .withAbilityName(cls.getName())
                .build();
        // 把operation设置到intent中
        intent.setOperation(operation);
        startAbility(intent);
    }

    private void navigateToSetPinAbility(boolean isRxExample) {
        SharedPrefs.setRxExample(isRxExample);
        Intent intent = new Intent();
        // 通过Intent中的OperationBuilder类构造operation对象，指定设备标识（空串表示当前设备）、应用包名、Ability名称
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("co.infinum.goldfinger.example")
                .withAbilityName("co.infinum.goldfinger.example.SetPinAbility")
                .build();
        // 把operation设置到intent中
        intent.setOperation(operation);
        startAbility(intent);
        terminateAbility();
    }
}
